import 'package:base_flutter/base_flutter.dart';
import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/net/api/http_api.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/util/image_utils.dart';

import '../../viewmodel/use_request/choose_goods_view_model.dart';

class ChooseGoodsPage extends BaseStatefulMvvmWidget {
  final dynamic useRange;

  ChooseGoodsPage({this.useRange = "0"});

  @override
  State<StatefulWidget> createState() => _ChooseGoodsState();
}

class _ChooseGoodsState
    extends BaseMvvmState<ChooseGoodsViewModel, ChooseGoodsPage> {
  @override
  Widget buildLoadingContentView() {
    return Row(
      children: [
        ListView.builder(
          itemBuilder: (context, index) {
            var item = viewModel.typeList[index];
            return Stack(
              alignment: Alignment.center,
              children: [
                CommonText(item.materialTypeName,
                        textSize: 14,
                        textColor: viewModel.selectType == item
                            ? Colors.blue
                            : Colors.black)
                    .addToContainer(
                        padding: EdgeInsets.symmetric(
                          vertical: 15,
                        ),
                        margin: EdgeInsets.symmetric(horizontal: 10)),
                if (viewModel.selectType == item)
                  Container(
                    width: 3,
                    height: 20,
                    decoration: BoxDecoration(
                        color: Colors.blue, borderRadius: 4.borderRadius),
                  ).setLocation(left: 0)
              ],
            ).onTap(() {
              viewModel.typeClick(item);
            }).addToContainer(
                color: viewModel.selectType == item
                    ? Colors.white
                    : Colours.bg_color);
          },
          itemCount: viewModel.typeList.length,
        ).addToContainer(color: Colours.bg_color).setWeight(1),
        (viewModel.goodsList.isNotEmpty
                ? ListView.separated(
                    itemBuilder: (context, index) {
                      var item = viewModel.goodsList[index];
                      var image = "${HttpApi.imageHead}${item.attatchId}";
                      return Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Stack(
                            children: [
                              ImageLoad(
                                item.attatchId == null
                                    ? ImageUtils.getImgPath("none")
                                    : image,
                                isAsset: item.attatchId == null,
                                errorImage: ImageUtils.getImgPath("none"),
                                width: 80,
                                height: 80,
                                fit: BoxFit.cover,
                              ).toRound(borderRadius: 10.borderRadius),
                              CommonText(item.assetTypeName ?? "",
                                      textSize: 10,
                                      fontWeight: FontWeight.bold,
                                      textColor: Colors.white)
                                  .addToContainer(
                                      padding: EdgeInsets.symmetric(
                                          horizontal: 4, vertical: 3),
                                      decoration: BoxDecoration(
                                          gradient: LinearGradient(colors: [
                                            Colors.blue,
                                            Colors.lightBlueAccent
                                          ], begin: Alignment.topLeft),
                                          borderRadius: 4.borderRadius))
                                  .setLocation(right: 3, top: 4)
                            ],
                          ).addToContainer(width: 80, height: 80),
                          Container(
                            width: 8,
                          ),
                          Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Container(
                                height: 5,
                              ),
                              RichText(
                                  text: TextSpan(
                                      text: "${item.materialName}",
                                      style: TextStyle(
                                          color: Colors.black, fontSize: 14),children: [
                                            if(!item.model.isNullOrEmpty())
                                            TextSpan(text: "  型号:${item.model}",style: TextStyle(fontSize: 12,color: Colors.grey))
                                  ])),

                              Container(height: 5,),
                              CommonText(item.branchName.isNullOrEmpty()?"":"维护机构：${item.branchName}",textSize: 12,textColor: Colors.grey[700]!),
                              Container(height: 5,),
                              CommonText(item.brand.isNullOrEmpty()?"":"品牌：${item.brand}",textSize: 12,textColor: Colors.grey[700]!),
                              Container(height: 5,),
                              Row(
                                children: [
                                  CommonText("库存：${item.total}${item.unit??""}",textSize: 12,).setWeight(1),
                                  TextButton(onPressed: (){
                                    if(item.total<=0){
                                      showToast("库存不足");
                                      return;
                                    }
                                    finish(result: item);
                                  }, child: CommonText("添加",textSize: 12,textColor: Colors.white),style: ButtonStyle(
                                    shape: WidgetStateProperty.all(StadiumBorder()),
                                    backgroundColor: WidgetStateProperty.all(Colors.blue),

                                  ),).addToContainer(height: 30)
                                ],
                              )
                            ],
                          ).setWeight(1)
                        ],
                      );
                    },
                    padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
                    separatorBuilder: (context, index) {
                      return Container(
                        height: 10,
                      );
                    },
                    itemCount: viewModel.goodsList.length)
                : Center(
                    child: CommonText("未获取到物资信息"),
                  ))
            .setWeight(3)
      ],
    );
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      appBar: AppBar(
        title: Text("选择物资"),
      ),
      body: loadingContentWidget,
    );
  }

  @override
  ChooseGoodsViewModel createViewModel() {
    return ChooseGoodsViewModel(context,useRange: widget.useRange);
  }

  @override
  void onRetryClick() {
    viewModel.getGoodsTypeList();
  }
}
